package com.djf;

import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * ShardingJDBC 分库分表演示应用启动类
 * 
 * @author djf
 * @since 2024-01-20
 */
@Slf4j
@SpringBootApplication
@MapperScan("com.djf.mapper")
public class ShardingJdbcApplication {

    public static void main(String[] args) throws UnknownHostException {
        ConfigurableApplicationContext application = SpringApplication.run(ShardingJdbcApplication.class, args);
        Environment env = application.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        String path = env.getProperty("server.servlet.context-path", "");
        
        log.info("\n----------------------------------------------------------\n\t" +
                "Application '{}' is running! Access URLs:\n\t" +
                "Local: \t\thttp://localhost:{}{}\n\t" +
                "External: \thttp://{}:{}{}\n\t" +
                "Profile(s): \t{}\n" +
                "----------------------------------------------------------",
                env.getProperty("spring.application.name"),
                port, path,
                ip, port, path,
                env.getActiveProfiles());
        
        log.info("\n----------------------------------------------------------\n\t" +
                "ShardingJDBC 分表测试接口:\n\t" +
                "生成测试数据: \tPOST http://localhost:{}{}/api/orders/test-data?count=20\n\t" +
                "查询订单: \t\tGET http://localhost:{}{}/api/orders/user/1\n\t" +
                "时间范围查询: \tGET http://localhost:{}{}/api/orders/time-range?startTime=2024-01-01 00:00:00&endTime=2024-12-31 23:59:59\n\t" +
                "分表信息: \t\tGET http://localhost:{}{}/api/orders/sharding-info\n" +
                "----------------------------------------------------------",
                port, path,
                port, path,
                port, path,
                port, path);
    }
}